/**
 * 
 */
package com.emar.adx.yiqifa.util;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Hashtable;

/**
 * 配置文件工厂类，能管理多个配置文件。
 * 
 * @author xiaoliang
 * @version 4.0.0
 * @see Configuration
 * @since 4.0.0 2014-11-11
 */
public class ConfigurationFactory {
	private static final Hashtable<String, Configuration> configs = new Hashtable<String, Configuration>(
			5);

	public static Configuration getConfiguration(String filename) {
		if(!filename.endsWith("properties")) {
			filename = filename + ".properties" ;
		}
		loadConfiguration(filename);
		return configs.get(filename);
	}

	private static void loadConfiguration(String filename) {
		if (!configs.containsKey(filename)) {
			Configuration config = new Configuration(filename);
			synchronized (configs) {
				if (!configs.containsKey(filename)) {
					configs.put(filename, config);
				}
			}
		}
	}

	public static String getProperty(String filename,String name) {
		String result = null;
		java.util.Properties props;
		try {
			props = PropertiesLoaderUtils.loadAllProperties(filename);
			result = props.getProperty(name);//根据name得到对应的value
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}
}
